import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.FileWriter;
import java.io.IOException; 

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JanelaRemover extends JPanel {
	JTextField texto = new JTextField(80);
    String texto2 = "" ;
    JLabel texto_label = new JLabel("Informe o nome: ");
    
    String array[] = new String[9];
    String data[] = new String[9];
    
    private static final int MAX = 11;
    
    public JanelaRemover() {
    	
    	
            
        JButton botao = new JButton();
        JButton voltar = new JButton();
    
        voltar.setText("Voltar");
        botao.setText("Excluir");
        
        
        botao.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
            	int i=0;
                int quant = 0;
                Object[][] data = null;
                FileWriter writer;
            
        try{
        	
            BufferedReader in = new BufferedReader(new FileReader("src/membroEJ.txt"));
            String str, str2;
            String array[] = new String[MAX];
            
            while ((str = in.readLine()) != null){
                    quant++;
            }
            i=0;
            data = new Object[quant][MAX];
            in = new BufferedReader(new FileReader("src/membroEJ.txt"));
            while ((str = in.readLine())!= null){
                    
                    	array = str.split("&");
                    	data[i][0] = array[0];
	                    data[i][1] = array[1];
	                    data[i][2] = array[2];
	                    data[i][3] = array[3];
	                    data[i][4] = array[4];
	                    data[i][5] = array[5];
	                    data[i][6] = array[6];
	                    data[i][7] = array[7];
	                    data[i][8] = array[8];
	                    data[i][9] = array[9];
	                    data[i][10] = array[10];
	                    i++;
                    
            }
            in.close();
            
            in = new BufferedReader(new FileReader("src/membroEJ.txt"));
            writer = new FileWriter("src/membroEJ_temp.txt", true);
            while ((str2 = in.readLine())!= null){
            	if(!data[i][0].equals(texto.getText())){
		            writer.write(data[i][0]+"&");
		            writer.write(data[i][1]+"&");
		            writer.write(data[i][2]+"&");
		            writer.write(data[i][3]+"&");
		            writer.write(data[i][4]+"&");
		            writer.write(data[i][5]+"&");
		            writer.write(data[i][6]+"&");
		            writer.write(data[i][7]+"&");
		            writer.write(data[i][8]+"&");
		            writer.write(data[i][9]+"&");
		            writer.write(data[i][10]+"\r\n");
		            i++;
            	}
            }
            writer.close();
            in.close();
        
        	        	
                             
                            
            
	        }catch (IOException e2){
	                e2.printStackTrace();
	        }
            }
        });
        
    
    voltar.addActionListener( new ActionListener() {
        
        public void actionPerformed(ActionEvent arg0) {
                        JanelaPrincipal.GetInstance().TrocaPainel(new JanelaInicial());
        }
    });
    
    this.setLayout( new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    
    c.gridwidth = 1;
    c.gridx = 0 ;
    c.gridy = 0;
    this.add(texto_label,c);
    
    
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 6;

    c.gridx = 4 ;
    c.gridy = 0;
    this.add(texto,c);
    
    c.gridx = 0 ;
    c.gridy = 5;
    c.gridwidth = 2;
    c.insets = new Insets(20,5,0,0) ;
    this.add(botao, c);
    
    c.gridx = 3 ;
    c.gridy = 5;
    c.insets = new Insets(20,5,0,0) ;
    c.gridwidth = 2;
    this.add(voltar, c);
    
    }

}
